﻿using Yewan.Modbus;

var rtu = new ModbusRtu("COM1", 115200);
rtu.Connect();

//{
//    //设定转速
//    //var result = rtu.Write(0x01, FunctionCode.WriteSingleRegister, 0x1002, 0x07A0);

//    //设定时间(分钟)
//    //var result = rtu.Write(0x01, FunctionCode.WriteSingleRegister, 0x1003, 0x0002);

//    //设定启停
//    var result = rtu.Write(0x01, FunctionCode.ReadInputRegisters, 0x1005, true);

//    var str2 = BitConverter.ToString(result);
//    Console.WriteLine($"写入内容：{str2}");
//}

//Thread.Sleep(1000);

{
    for (int i = 0; i < 200; i++)
    {
        var result = rtu.Read(0x01, FunctionCode.ReadInputRegisters, 0x9C41, 0x0002);

        //byte转十六进制字符串
        //var str1 = Convert.ToHexString(result);
        //Console.WriteLine(str1);

        var str2 = BitConverter.ToString(result);
        Console.WriteLine($"接收内容：{str2}");

        var bytes1 = ByteOrderHelper.SwitchByteOrder([result[1], result[2]], ByteOrderType.DCBA);
        var value1 = BitConverter.ToInt16(bytes1);
        Console.WriteLine($"参数值1：{value1}");

        var bytes2 = ByteOrderHelper.SwitchByteOrder([result[3], result[4]], ByteOrderType.DCBA);
        var value2 = BitConverter.ToInt16(bytes2);
        Console.WriteLine($"参数值2：{value2}");

        Thread.Sleep(500);
    }
}


Console.ReadLine();
